package com.flute.framework.autoprompt;

import java.util.Hashtable;
import java.util.List;
import java.util.Map.Entry;

import com.flute.tools.util.FileUtil;
/**
 * 
 * @author Jazz
 * 用于search自动弹出框搜索
 */
public final class AutoPromptSearcher {
	private AutoPrompt autpPrompt;
	private static AutoPromptSearcher instance;

	private AutoPromptSearcher(String key) {
		this.autpPrompt = AutoPrompt.getInstance();
		Thread t = new Thread(getRunnable());
		t.start();
	}

	public synchronized static AutoPromptSearcher getInstance(String key) {
		if (instance == null) {
			instance = new AutoPromptSearcher(key);
		}

		return instance;
	}

	public AutoPrompt getAutoPrompt() {
		return autpPrompt;
	}

	private Runnable getRunnable() {
		return new Runnable() {

			@Override
			public void run() {
				Hashtable<String, Integer> map = new Hashtable<String, Integer>();
				String[] lines = FileUtil.getContentByLine(this.getClass()
						.getResourceAsStream("r.txt"), "GBK");
				int i = 0;
				for (String line : lines) {
					if (line.contains(",")) {
						int index = line.lastIndexOf(",");
						String s = line.substring(0, index);
						map.put(s, Integer.valueOf(line.substring(index + 1)));
						if (i++ % 10000 == 0) {
							System.out.println(i);
						}
					}
				}

				i = 0;
				for (Entry<String, Integer> entry : map.entrySet()) {
					if (i++ % 10000 == 0) {
						System.out.println("train:" + i);
					}
					List<TrainningToken> tl = new CommonPriorityStrategy()
							.getWordResult(entry.getKey());
					for (TrainningToken token : tl) {
						token.getToken().updatePriority(entry.getValue(), 2);
						// autpPrompt.train(token);
					}
					if (i > 1000000) {
						break;
					}
				}
				System.out.println("trainning done");
			}

		};
	}
}
